package com.xiaoyg.algorithm;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 队列实现栈
 */
public class Queue2Stack {
    private Queue<Integer> queue;
    public Queue2Stack(){
        queue = new LinkedList<>();
    }
    public void push(Integer e){
        //队尾入列
        queue.offer(e);
        //获取队列的大小，将队尾元素前面的n-1个元素，挪到队尾，使新加入元素挪到对手
        int size = queue.size();
        int i = 0;
        while (i < size -1){
            queue.offer(queue.poll());
            i++;
        }
    }
    //出栈
    public Integer pop(){
        return queue.poll();
    }
    public Integer peek(){
        return queue.peek();
    }
    public boolean isEmpty(){
        return queue.isEmpty();
    }
    public static void main(String[] args) {
        Queue2Stack stack = new Queue2Stack();
        stack.push(1);
        System.out.println(stack.peek());
        stack.push(2);
        System.out.println(stack.peek());
        stack.push(3);
        System.out.println(stack.peek());

        System.out.println("=================");
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());
    }


}
